/******************************************************************************
秋夜静，独坐对残灯。
啼笑非非谁识我，坐行梦梦尽缘君，何所慰消沉。
风卷雨，雨复卷侬心。
心似欲随风雨去，茫茫大海任浮沉。
无爱亦无恨。
******************************************************************************/
#include "rgb_thread.h"
#include "rgb_thread_keyboard.h"
#include <QDebug>
#include "Curve/hsv.h"
#include "Curve/Curve.h"
//#include "Effects/kbRGB/kbRGB.h"
#include "Effects/kbRGB_Int/kbRGB_Int.h"

extern "C" uint32_t tb_get_sys_time_ms(void);
#ifdef KB_ROWS
#undef  KB_ROWS
#endif
#ifdef KB_COLS
#undef  KB_COLS
#endif
#if 1
#define KB_ROWS KBRGB_ANGLE_SIZE
#else
#define KB_ROWS 6
#endif
#define KB_COLS KBRGB_ANGLE_SIZE

void sin_test(void)
{
    int16_t angle;
    int16_t _Sin=0;
    float _sin=0;
    printf("static const float SinTable[] = {\r\n");
    for(angle=0; angle<360; angle++)
    {
        _Sin = kbRGB_Sin(angle<<KBRGB_SIN_PRE);
        _sin = _Sin/255.0f;
        printf("%9f, ",  _sin);
        //printf("%3d, ",  _Sin);
        if((angle>0) && (9==(angle%10))) printf("\r\n");
        if((angle>0) && (89==(angle%90))) printf("\r\n");
    }
    printf("\r\n};\r\n");
//    for(angle=89; angle<93; angle++)
//    {
//        _Sin = kbRGB_Sin(angle<<KBRGB_SIN_PRE);
//        _sin = _Sin/255.0f;
//        //printf("%9f, ",  _sin);
//        printf("angle[%3d] = %3d\r\n", angle, _Sin);
//    }
}
//void angle_test(void)
//{
//    int16_t x, y;
//    struct kbRGB_Pixel piont = {.angle=0, .radius=0xFFFF};
//    printf("static const uint16_t angle[MATRIX_ANGLE_SIZE][MATRIX_ANGLE_SIZE] = {\r\n");
//    for(x=-KBRGB_ANGLE_SIZE/2; x<KBRGB_ANGLE_SIZE/2; x++)
//    {
//        printf("{");
//        for(y=-KBRGB_ANGLE_SIZE/2; y<KBRGB_ANGLE_SIZE/2; y++)
//        {
//            piont.pixel = kbRGB_Angle_IntGet(x, y);
//            printf("%3d, ",  piont.angle>>KBRGB_ANGLE_INT_PRE);
//        }
//        printf("},\r\n");
//    }
//    printf("\r\n};\r\n");
//    fflush(stdout);
//}
void cos_test(void)
{
    int16_t angle;
    float _cos=0;
    printf("static const float CosTable[] = {\r\n");
    for(angle=0; angle<360; angle++)
    {
        _cos = (float)kbRGB_Cos(angle<<KBRGB_SIN_PRE);
        _cos = _cos/255.0f;
        printf("%9f, ",  _cos);
        if((angle>0) && (9==(angle%10))) printf("\r\n");
    }
    printf("\r\n};\r\n");
    fflush(stdout);
}
#define xxCK	0x00
#define xxSK	0x00
#define  FN                0xF0  // 扩展码
//#define  Fn                0x80    // 键码
#define  Fn                (FN|0x00) // Fn key
#define  LCTRL     (FN|0x01)  // 0x01
#define  LSHIFT    (FN|0x02)  // 0x02
#define  LALT      (FN|0x03)  // 0x04
#define  LWIN      (FN|0x04)  // 0x08
#define  RCTRL     (FN|0x05)  // 0x10
#define  RSHIFT    (FN|0x06)  // 0x20
#define  RALT      (FN|0x07)  // 0x40
#define  RGUI      (FN|0x08)  // 0x80  Fn key
// RGUI键 即为 Fn键
// key_board_map_value
static const uint8_t kb_def_map_value[6][21] = {
      {0x29,   xxSK, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,   0x46, 0x47, 0x48, xxSK, xxSK, xxSK, xxSK},
      {0x35,   0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2D, 0x2E, 0x2A,   0x49, 0x4A, 0x4B, 0x53, 0x54, 0x55, 0x56},
      {0x2B,   0x14, 0x1A, 0x08, 0x15, 0x17, 0x1C, 0x18, 0x0C, 0x12, 0x13, 0x2F, 0x30, 0x31,   0x4C, 0x4D, 0x4E, 0x5F, 0x60, 0x61, 0x57},
      {0x39,   0x04, 0x16, 0x07, 0x09, 0x0A, 0x0B, 0x0D, 0x0E, 0x0F, 0x33, 0x34, 0x00, 0x28,   xxSK, xxSK, xxSK, 0x5C, 0x5D, 0x5E, xxSK},
      {LSHIFT, 0x00, 0x1D, 0x1B, 0x06, 0x19, 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0x00, RSHIFT, xxSK, 0x52, xxSK, 0x59, 0x5A, 0x5B, 0x58},
      {LCTRL,  LWIN, LALT, xxSK, xxSK, xxSK, 0x2C, xxSK, xxSK, xxSK, RALT, Fn,   0x65, RCTRL,  0x50, 0x51, 0x4F, 0x62, xxSK, 0x63, xxSK},
};
static void kb_def_map(uint8_t cols, uint8_t rows)
{
    uint8_t col, row;
    uint32_t kb104_map_mask[6];
    uint32_t mask;
    if(cols>32) cols=32;
    if(rows>6)  rows=6;
    printf("const uint32_t kb104_map_mask[%d] = {", rows);
    for(row=0; row<rows; row++)
    {
        mask = 0;
        for(col=0; col<cols; col++)
        {
            if(0x00!=kb_def_map_value[row][col]) mask |= (0x1<<col);
        }
        kb104_map_mask[row] = mask;
        printf("0x%08X, ",  kb104_map_mask[row]);
    }
    printf("};\r\n");
    fflush(stdout);
}
extern "C" uint16_t kb_rand(void)
{
    return rand();
}
extern "C" void RGB_Slave_Test(void);
void RGB_ThreadKeyBoard::loop()
{
    //struct kbRGB_Effect_t const* effect=NULL;
    int16_t index=0;
    //uint32_t cnt=0;
    uint32_t kb_tick=0;
    const uint32_t kb_delay=5000; // ms
    uint32_t tick=0;
    int FPS;
    if(0==isrun())
    {
        QThread::msleep(100);
        return;
    }
    //kb_def_map(KB_COLS, KB_ROWS);
    //kbRGB_Init();
    //kbRGB_Int_Init();
    //kbRGB_Int_Init(index);
    //kbRGB_Angle2();
    //kbRGB_Angle8bit();
    //kbRGB_Sin_Cal();
    //kbRGB_Cos_Cal();
    //kbRGB_Angle2_Int();
    //sin_test();
    //cos_test();
    //angle_test();
    RGB_Slave_Test();
    index=0;//14;
    kbRGB_Int_Init(index);

    tick = tb_get_sys_time_ms();
    kb_tick = tick+kb_delay;
    //const uint32_t reverseX = 0x55555555;   //x反向
    //const uint32_t reverseY = 0x55555555;   //y反向
//    const uint32_t reverseX = 0xFFFFFFFF;   //x反向
//    const uint32_t reverseY = 0xFFFFFFFF;   //y反向
    const uint32_t reverseX = 0x55555555;//x55555555;   //x反向
    const uint32_t reverseY = 0x00;//xFFFFFFFF;   //y反向
    while(isrun())
    {
        int cx = ((Controller.matrix_map_width()-KB_COLS)>>1);
        int cy = ((Controller.matrix_map_width()-KB_ROWS)>>1);
        int32_t col_id, row_id;
        int32_t x, y;
        //qDebug("[%s--%d] RGB_ThreadKeyBoard cnt:%d ", __func__, __LINE__, cnt++);
        //effect->Effect(KB_COLS, KB_ROWS);
        FPS = kbRGB_Int_Effects(index, KB_COLS, KB_ROWS, 0, 1, 0);
        if(FPS<0) FPS=100;
#if 1
        for(col_id=0; col_id<KB_COLS; col_id++)
        {
            for(row_id=0; row_id<KB_ROWS; row_id++)
            {
                //Controller.SetLED(x, y, kbRGB_get_point(x, y), 0);
                if((reverseY>>col_id)&0x1) y=KB_ROWS-1-row_id;
                else y=row_id;
                if((reverseX>>row_id)&0x1) x=KB_COLS-1-col_id;
                else x=col_id;
                Controller.SetLED(x+cx, y+cy, kbRGB_get_point(col_id, row_id), 0);
            }
        }
#else
        for(col_id=0; col_id<KB_COLS; col_id++)
        {
            for(row_id=0; row_id<KB_ROWS; row_id++)
            {
                //Controller.SetLED(x, y, kbRGB_get_point(x, y), 0);
                if((reverseY>>col_id)&0x1) y=KB_ROWS-1-row_id;
                else y=row_id;
                if((reverseX>>row_id)&0x1) x=KB_COLS-1-col_id;
                else x=col_id;
                Controller.SetLED(y+cy, x+cx, kbRGB_get_point(col_id, row_id), 0);
            }
        }
#endif
        QThread::msleep(FPS);
        tick = tb_get_sys_time_ms();
        //cnt++;
        if(kb_tick<=tick)//if(cnt>200)   // 25*100=2.5s
        {
            //cnt=0;
            kb_tick = tick+kb_delay;
#if 0
            //index++;
            //if(index>=KB_RGB_EFFECTS) index=0;
            index = kbRGB_Int_Init(index+1);
            if(index<0)
            {
                index=0;
                kbRGB_Int_Init(index);
            }
            qDebug("[%s--%d] RGB_ThreadKeyBoard index:%d ", __func__, __LINE__, index);
            //effect=&kbrgb_effect[index];
            //kbRGB_Params_Init();
            //effect->Init();
            //kbRGB_Int_Init(index);
#endif
        }
    }
}

